<?php

include_once ('bn_path.php');

class bn_post
{
    private $dl;
    private $gf;
    protected static $instance;
    public $post_custom;

    public function __construct()
    {
        $this->dl = new bn_dal;
        $this->gf = new bn_genralfunc;
        $this->post_custom=new bn_post_custom;
    }


    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function insert($cat_id, $user_id, $title, $text, $comment, $status)
    {
        return $this->dl->insert('posts', array($cat_id,$user_id,$title,$this->gf->date(),$text,$comment,null,null,$status));
    }

    public function update($cat_id, $title, $text, $user_id, $id)
    {
        $this->dl->update('posts', array(
            'cat_id' => $cat_id,
            'title' => $title,
            'text' => $text,
            'modif_uid' => $user_id,
            'modif_date' => $this->gf->date()), array('id=' => $id));
    }

    public function update_col($col, $val, $id)
    {
        $this->dl->update('posts', array($col => $val), array('id=' => $id));
    }

    public function delete($id)
    {
        $this->dl->delete('posts', $id);
    }

    public function select_allposts($col = '', $val = '') 
    {
        if (empty($col) && empty($val))
            return $this->dl->select('SELECT (p.id)id,(u.user)user,cat,cat_id,title,p.date,comment,text,modif_date,(v.user)modif_user,status FROM posts p join category b on cat_id=b.id join users u on user_id=u.id  left join users v on modif_uid=v.id');
        else
        {
            if ($col == 'user')
                $col = "u.$col";
            elseif ($col == 'modif_user')
                $col = 'v.user';
            else
                $col = "p.$col";
            return $this->dl->select("SELECT (p.id)id,(u.user)user,cat,cat_id,title,p.date,comment,text,modif_date,(v.user)modif_user,status FROM posts p join category b on cat_id=b.id join users u on user_id=u.id  left join users v on modif_uid=v.id where $col=?",
                $val);
        }
    }

    public function select_allposturl($cat_id='')
    {
        if($cat_id=='')
        return $this->dl->select('select title,text,p.date,user,concat("' .bn_Get_sitename() . root .'/post.php?pid=",convert(p.id,char(10)))url from posts p join users u on user_id=u.id');
        else
        return $this->dl->select('select title,text,p.date,user,concat("' .bn_Get_sitename() . root .'/post.php?pid=",convert(p.id,char(10)))url from posts p join users u on user_id=u.id and cat_id=?',$cat_id);
    }


    public function get_post($id)
    {
        return $this->dl->getrow('select p.id,title,text,cat_id,p.date,user,comment from posts p join users u on user_id=u.id and p.id=?',$id);
    }

    public function get_post_id($get = 'pid')
    {
        if (isset($_GET[$get]))
            return $this->dl->getvalue('select id from posts where id=? and status="enable"',
                $this->gf->Get_number($_GET[$get]));
    }

    public function __destruct()
    {
        unset($this->dl);
        unset($this->gf);
    }
}

?>